﻿using System.Reflection;

namespace Dotnet.Utils.Utility.AssemblyUtil
{
    /// <summary>
    /// 属性扩展类
    /// </summary>
    public static class PropertyInfoExtension
    {
        public static readonly BindingFlags bindingAttr = BindingFlags.Static | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase;

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public static object GetPropertyValue(this object obj, string propertyName)
        {
            PropertyInfo property = obj.GetType().GetProperty(propertyName, bindingAttr);
            return property.GetValue(obj);
        }

        /// <summary>
        /// 设置属性值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue(this object obj, string propertyName, object value)
        {
            PropertyInfo property = obj.GetType().GetProperty(propertyName, bindingAttr);
            property.SetValue(obj, value);
        }
    }
}